﻿/*
 * Created by SharpDevelop.
 * User: Radiation
 * Date: 2017/12/1
 * Time: 17:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace autosettime
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		functions fc = new functions();
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void GetServerTimeClick(object sender, EventArgs e)
		{
			string url = serverAddress.Text;
			string type = accessType.Text;
			if(url=="" || type==""){
				MessageBox.Show("请确保每项都不为空");
				return;
			}
			
			string result = fc.getServerTime(url,type);
			DateTime now;
			if(result.Length!=0){
				if(result.Substring(0,5)!="Error"){
					//DateTime dt = DateTime.ParseExact(result,"yyyy年M月d日 H:mm:ss",System.Globalization.CultureInfo.CurrentCulture);
					try{
						DateTime.TryParse(result,out now);
						label4.Text=now.ToString("yyyy年M月d日 H:mm:ss");
						//MessageBox.Show(now.ToString());
					}catch(Exception ex){
						MessageBox.Show("格式转换错误!"+ex.Message);
						return;
					}
				}else{
					MessageBox.Show(result);
				}
			}
		}
		void LocalTimeSetClick(object sender, EventArgs e)
		{
			string url = serverAddress.Text;
			string type = accessType.Text;
			if(url=="" || type==""){
				MessageBox.Show("请确保每项都不为空");
				return;
			}
			if (MessageBox.Show("您真的确定更改系统当前日期和时间吗？", "信息提示", MessageBoxButtons.OK) == DialogResult.OK){
				string result = fc.getServerTime(url,type);
				DateTime now;
				if(result.Length!=0){
					if(result.Substring(0,5)!="Error"){
						try{
							DateTime.TryParse(result,out now);
							//label4.Text=now.ToString("yyyy年M月d日 H:mm:ss");
							string stype = setType.Text;
							if(stype=="DOS"){
								fc.dosToSetLocalDate(now);
							}else if(stype=="Win Api"){
								fc.dosToSetLocalDate(now);
							}else{
								MessageBox.Show("错误的传值!");
								return;
							}
							MessageBox.Show("时间设置完成!如果没有成功请以管理员方式运行再次尝试！");
						}catch(Exception ex){
							MessageBox.Show("格式转换错误!"+ex.Message);
							return;
						}
					}else{
						MessageBox.Show(result);
					}
				}
			}
			
		}
	}
}
